const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../utils/config.js'
import {
    validateEmoji,
    validateSpecial
} from '../../../utils/validate.js'
import {
    reqSpaceEdit,
    reqSpaceImage,
    reqDeviceBySpaceId,
    reqDeviceByImei,
    reqDeviceAdd,
    reqDeviceUnBind,
    reqAlarmReceiverList,
    reqGetBySpaceId,
    reqDeviceGroupadd,
    reqdeviceGroupupdate,
    reqdeviceGroupremove
} from '../../../request/api'
Page({
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        parkId: '', //园区id
        alarmReceiverList: [],
        alarmPage: 1,
        alarmIsNextPage: true,
        alarmReceiverArr: [], //最终提交值id集合
        spaceId: "", //安装点id
        id: "", //设备组id
        imei: []
    },
    onLoad(e) {
        // console.log(JSON.parse(e.item));
        this.getItemInfo(e)
    },
    onShow() {
        this.GetBySpaceId() //安装点id查询设备组
        this.getAlarmReceiverBySpaceId() //空间id获取安装设备
    },
    // 获取信息
    getItemInfo(e) {
        let item = JSON.parse(e.item)
        console.log(item);
        this.setData({
            // parkId: item.parkId,
            spaceId: item.id,
        })
    },
    // 空间id获取安装设备
    async getAlarmReceiverBySpaceId() {
        const res = await reqDeviceBySpaceId(this.data.spaceId)
        let {
            rows
        } = res.data
        let arr = []
        rows.forEach((item, index) => {
            // console.log(item);
            if (item.deviceType == 'DoorMagnetism' || item.deviceType == 'Infrared') {
                arr.push({
                    id: index,
                    devicePrimaryId: item.id,
                    imei: item.imei,
                    type: item.productName,
                    deviceType: item.deviceType
                })
            }
        })
        this.setData({
            imeiList: arr
        })
    },
    //安装点id查询设备组
    async GetBySpaceId() {
        const res = await reqGetBySpaceId(this.data.spaceId)
        console.log(res);
        if (res.data.row == '' || res.data.row == null) {
            this.setData({
                id: "",
                imei: []
            })
        } else {
            this.setData({
                id: res.data.row.id,
                alarmReceiverArr: res.data.row.imei.split(','),
                imei: res.data.row.imei
            })
        }
    },
    /**
     * @name: 获取接警人列表
     * @return {*}
     */
    // async getAlarmReceiverList() {
    //     let params = {
    //         orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
    //     }
    //     const res = await reqAlarmReceiverList(this.data.alarmPage, 99, params)
    //     if (res.code !== 20000) return showMsg(res.message, "error")
    //     let {
    //         rows
    //     } = res.data
    //     console.log(rows);
    //     if (this.data.alarmPage === 1) {
    //         this.setData({
    //             alarmReceiverList: rows,
    //             alarmIsNextPage: true
    //         })
    //     } else if (this.data.alarmPage > 1 && rows.length) {
    //         this.setData({
    //             alarmReceiverList: this.data.alarmReceiverList.concat(rows),
    //             alarmIsNextPage: true
    //         })
    //     } else if (this.data.alarmPage > 1 && !rows.length) {
    //         showMsg('已经到底啦~')
    //         this.setData({
    //             alarmIsNextPage: false
    //         })
    //     }
    // },

    // 点击获取alarmReceiverArr的值
    deviceToggle(event) {
        const {
            id
        } = event.currentTarget.dataset;
        this.checkboxChange(id)
    },
    // 复选框变化渲染
    checkboxChange(id) {
        // console.log(id);
        const checkbox = this.selectComponent(`.checkboxes-${id}`);
        checkbox.toggle();
    },
    // 获取alarmReceiverArr的值
    deviceChange(event) {
        this.setData({
            alarmReceiverArr: event.detail
        })
    },

    // 确认
    async confirm() {
        if (this.data.alarmReceiverArr.length == 0 && this.data.id != '') {
            // 删除
            const res = await reqdeviceGroupremove(this.data.id)
            // console.log(res);
            if (res.code != 20000) return showMsg(res.message);
            showMsg("删除设备组成功！", 'success')
            this.GetBySpaceId() //安装点id查询设备组
            return;
        }

        if ((this.data.id == '' || (this.data.alarmReceiverArr.length > 0 && this.data.id != '')) && this.data.alarmReceiverArr.length != 4) {
            return showMsg('请选择两台门磁，红外联动告警');
        }
        let num = 0
        this.data.imeiList.forEach(item => {
            this.data.alarmReceiverArr.forEach(item2 => {
                if (item.imei == item2 && item.deviceType == 'Infrared') {
                    num++;
                }
            })
        })
        // console.log(num);
        if (num != 2) return showMsg('请选择两台门磁，红外联动告警');

        if (this.data.id == '') {
            // 新增
            const res = await reqDeviceGroupadd({
                spaceId: this.data.spaceId,
                imei: this.data.alarmReceiverArr.join()
            })
            if (res.code != 20000) return showMsg(res.message);
            showMsg("新增设备组成功！", 'success')
            this.GetBySpaceId() //安装点id查询设备组
        } else if (this.data.alarmReceiverArr.length > 0 && this.data.id != '') {
            // 编辑
            const res = await reqdeviceGroupupdate({
                id: this.data.id,
                imei: this.data.alarmReceiverArr.join()
            })
            if (res.code != 20000) return showMsg(res.message);
            showMsg("更新设备组成功！", 'success')
            this.GetBySpaceId() //安装点id查询设备组
        }
    },

    // 滚动到底部触发
    // alarmReceiverLower() {
    //     if (!this.data.alarmIsNextPage) return
    //     this.setData({
    //         alarmPage: this.data.alarmPage + 1,
    //     })
    //     this.getAlarmReceiverList()
    // },
})